package com.liutaw.nanjingair.views.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.liutaw.nanjingair.R;
import com.liutaw.nanjingair.views.activities.PDFReaderActivity;
import com.liutaw.nanjingair.views.activities.products.WeatherWaringActivity;
import com.szjlxh.domain.model.product.WarningInfo;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by liutao on 16/3/27.
 */
public class WarningAdapter extends BaseAdapter {
    private Context context;
    private List<WarningInfo> warningInfos;

    public WarningAdapter(Context context, List<WarningInfo> warningInfos) {
        this.context = context;
        this.warningInfos = warningInfos;
    }

    @Override
    public int getCount() {
        return warningInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return warningInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_warninginfo, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }
        viewHolder = (ViewHolder) convertView.getTag();
        viewHolder.textTitle.setText(warningInfos.get(position).getWarninginfo());
        viewHolder.textTime.setText(warningInfos.get(position).getTime());
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(PDFReaderActivity.getCallingIntent(context,warningInfos.get(position).getPath()));
            }
        });

        return convertView;
    }

    public static final class ViewHolder {
        @Bind(R.id.img_left)
        ImageView imgLeft;
        @Bind(R.id.text_title)
        TextView textTitle;
        @Bind(R.id.text_time)
        TextView textTime;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public void addWarningInfos(List<WarningInfo> warningInfos) {
        this.warningInfos.addAll(warningInfos);
        notifyDataSetChanged();
    }
}
